package com.pai4j.pgc.controller.community;
import com.pai4j.common.bean.PAIPageResponseBeanUtil;
import com.pai4j.common.bean.PAIResponseBean;
import com.pai4j.common.bean.PAIResponseBeanUtil;
import com.pai4j.common.config.annotation.RequestPermission;
import com.pai4j.common.helper.SessionHelper;
import com.pai4j.domain.community.vo.request.CCPathSaveRequestVO;
import com.pai4j.domain.community.vo.response.cc.CTCPathBaseVO;
import com.pai4j.domain.community.vo.response.cc.CTCPathDetailVO;
import com.pai4j.pgc.service.community.CCPathAdminService;
import com.pai4j.pgc.service.community.CCPathService;
import com.pai4j.remote.pgc.community.CCPathClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *  技术社区首页栏目
 *
 * @author: CYM-pai
 * @date: 2025/10/26 21:52
 **/
@RestController
public class CCPathController implements CCPathClient {
    
    @Autowired
    private CCPathService pathService;
    @Autowired
    private CCPathAdminService pathAdminService;
    
    @Override
    @RequestPermission
    public PAIResponseBean<String> initPathAdmin() {
        pathAdminService.initPathAdmin();
        return PAIResponseBeanUtil.success("ok");
    }
    
    @Override
    @RequestPermission
    public PAIResponseBean<Long> save(@RequestBody CCPathSaveRequestVO pathSaveRequest) {
        String userId = SessionHelper.getCurrentUserId();
        return PAIResponseBeanUtil.success(pathService.save(pathSaveRequest, userId));
    }
    
    @Override
    public PAIResponseBean<List<CTCPathBaseVO>> recommends() {
        return PAIResponseBeanUtil.success(pathService.getRecommendCTCPathFromCache(6));
    }
    
    @Override
    public PAIResponseBean<List<CTCPathBaseVO>> subscribes() {
        return PAIResponseBeanUtil.success(pathService.getSubscribesPathListFromCache(SessionHelper.getCurrentUserId()));
    }
    
    @Override
    public PAIResponseBean<List<CTCPathDetailVO>> getNewList() {
        return PAIResponseBeanUtil.success(pathService.getNewCTCPathListFromCache());
    }
    
    @Override
    public PAIResponseBean<List<CTCPathDetailVO>> getUserAttendPathList(@RequestParam(value = "size", defaultValue = "100") Integer size){
        String userId = SessionHelper.getCurrentUserId();
        return PAIResponseBeanUtil.success(pathService.getUserAttendPathListFromCache(userId, size));
    }
    
    /**
     * 更新栏目状态
     *
     * @param pathId
     * @param status
     * @return
     */
    @Override
    @RequestPermission
    public PAIResponseBean<Long> updateStatus(@PathVariable Long pathId, @PathVariable String status) {
        String userId = SessionHelper.getCurrentUserId();
        pathService.updateStatus(pathId, status, userId);
        return PAIResponseBeanUtil.success();
    }
    
    /**
     * 开通付费订阅
     *
     * @param pathId
     * @param money
     * @return
     */
    @Override
    @RequestPermission
    public PAIResponseBean<Long> updatePaidSubMoney(@PathVariable Long pathId, @PathVariable Integer money) {
        pathService.updatePaidSubMoney(pathId, money);
        return PAIResponseBeanUtil.success();
    }
    
    @Override
    @RequestPermission
    public PAIResponseBean<Long> recommend(@PathVariable Long pathId) {
        String userId = SessionHelper.getCurrentUserId();
        pathService.recommend(pathId, userId);
        return PAIResponseBeanUtil.success();
    }
    
    @Override
    @RequestPermission
    public PAIResponseBean<Long> cancelRecommend(@PathVariable Long pathId) {
        String userId = SessionHelper.getCurrentUserId();
        pathService.cancelRecommend(pathId, userId);
        return PAIResponseBeanUtil.success();
    }
    
    @Override
    @RequestPermission
    public PAIPageResponseBeanUtil<List<CTCPathBaseVO>> page(@RequestParam(value = "name", required = false) String name,
                                                             @RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                                                             @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        return pathService.pageList(name, pageNo, pageSize);
    }
    
    @Override
    public PAIResponseBean<List<CTCPathBaseVO>> feed(@RequestParam(value = "size", defaultValue = "8", required = false) Integer size) {
        int maxSize = size > 10 ? 10 : size;
        return PAIResponseBeanUtil.success(pathService.feed(SessionHelper.getCurrentUserId(), maxSize));
    }
}
